package com.yy.controller;
import java.io.BufferedReader;
import java.io.PrintWriter;
import java.util.Enumeration;

import com.opensymphony.xwork2.ActionSupport;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.json.JSONArray;
import org.json.JSONObject;

import com.yy.dto.CompanyDTO;
import com.yy.dto.JobDTO;
import com.yy.dto.ResumeDTO;
import com.yy.logic.IJobLogic;
import com.yy.logic.IResumeLogic;
import com.yy.logic.impl.ResumeLogic;

public class ResumeController {
	private static final long serialVersionUID = 1L;
	@Resource
	private IResumeLogic resumelogic;
	@Resource
	private IJobLogic jobLogic;
	
	private ResumeDTO resumeDTO;
	
	public ResumeDTO getResumeDTO() {
		return resumeDTO;
	}
	public void setResumeDTO(ResumeDTO resumeDTO) {
		this.resumeDTO = resumeDTO;
	}
	public void getAllResume() throws Exception{
		HttpServletResponse response = ServletActionContext.getResponse();//取得reponse实例
		response.setCharacterEncoding("utf-8");//设置输出的编码
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();//得到printwriter实例
		HttpServletRequest request=ServletActionContext.getRequest();
		System.out.println("THIS IS ResumeUser  1*********");
		String results=request.getParameter("JobId");
		int jobid=Integer.parseInt(results);
		try{
			System.out.println("THIS IS ResumeUser  1*********");
			out.print(this.resumelogic.getAllResume(jobid));
			System.out.println("THIS IS ResumeUSer  2*********");
		}catch (Exception e) {
			e.printStackTrace();
		} finally {
			out.flush();
			out.close();
		}
	}
	public void getCompanyJob()throws Exception{
		HttpServletResponse response = ServletActionContext.getResponse();//取得reponse实例
		response.setCharacterEncoding("utf-8");//设置输出的编码
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();//得到printwriter实例
		HttpServletRequest request=ServletActionContext.getRequest();
		System.out.println("THIS IS Resume  job1*********");
		String companyLoginName=request.getParameter("CompanyLoginName");
		int companyId;
		try{
			
			companyId=this.resumelogic.getCompanyId(companyLoginName);
			out.println(this.jobLogic.getAllJob(companyId));
			System.out.println("THIS IS Resume  job2*********");
		}catch (Exception e) {
			e.printStackTrace();
		} finally {
			out.flush();
			out.close();
		}
	}
	
	public void addResume() throws Exception{
		ResumeDTO resumeDTO = new ResumeDTO();
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		HttpServletRequest request=ServletActionContext.getRequest();
		System.out.println("********This is addResume controller**********");
		request.setCharacterEncoding("UTF-8");
		StringBuilder sb = new StringBuilder();
		BufferedReader reader = request.getReader();
		char[]buff = new char[1024];
		int len;
		while((len = reader.read(buff)) != -1) {
			  sb.append(buff,0, len);
		}
		System.out.println(sb.toString());
		String mark = sb.toString();
		System.out.println(mark);
		System.out.println(mark.length());
		JSONObject dataJson = new JSONObject(mark);
		String fName = dataJson.getString("name");
		String fSex = dataJson.getString("sex");
		String fSchool = dataJson.getString("school");
		String fPhone = dataJson.getString("phone");
		String fIntro = dataJson.getString("intro");
		int fJobId = dataJson.getInt("jobId");
		resumeDTO.setResumeName(fName);
		resumeDTO.setJobId(fJobId);
		resumeDTO.setResumeSex(fSex);
		resumeDTO.setResumeSchool(fSchool);
		resumeDTO.setResumePhone(fPhone);
		resumeDTO.setResumeIntro(fIntro);
		System.out.println(resumeDTO.getResumeIntro());
		System.out.println(resumeDTO.getResumePhone());
		System.out.println(resumeDTO.getResumeSex());
		System.out.println(resumeDTO.getJobId());
		out.println(this.resumelogic.addResume(resumeDTO));
	}
	public void addCommand() throws Exception{
		ResumeDTO resumeDTO = new ResumeDTO();
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		HttpServletRequest request=ServletActionContext.getRequest();
		System.out.println("********This is addCommand controller**********");
		request.setCharacterEncoding("UTF-8");
		StringBuilder sb = new StringBuilder();
		BufferedReader reader = request.getReader();
		char[]buff = new char[1024];
		int len;
		while((len = reader.read(buff)) != -1) {
			  sb.append(buff,0, len);
		}
		System.out.println(sb.toString());
		String mark = sb.toString();
		System.out.println(mark);
		System.out.println(mark.length());
		JSONObject dataJson = new JSONObject(mark);
		int resumeId = dataJson.getInt("resumeId");
		String resumeState=dataJson.getString("state");
		resumeDTO.setResumeId(resumeId);
		resumeDTO.setResumeState(resumeState);
		
		System.out.println("resumeId+"+resumeId);
		System.out.println("resumeState+"+resumeState);
		out.println(this.resumelogic.addCommand(resumeId,resumeState));
	}
	
	public void getResumuByName() throws Exception{
		HttpServletResponse response = ServletActionContext.getResponse();//取得reponse实例
		response.setCharacterEncoding("utf-8");//设置输出的编码
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();//得到printwriter实例
		HttpServletRequest request=ServletActionContext.getRequest();
		String userName = request.getParameter("userName");
		System.out.println("------------->+"+userName);
		System.out.println("THIS IS ResumeUser  1*********");
		//String results=request.getParameter("JobId");
		//int jobid=Integer.parseInt(results);
		try{
			System.out.println("THIS IS ResumeUser  1*********");
			out.println(this.resumelogic.getResumuByName(userName));
			//out.print(this.resumelogic.getAllResume(jobid));
			System.out.println("THIS IS ResumeUSer  2*********");
		}catch (Exception e) {
			e.printStackTrace();
		} finally {
			out.flush();
			out.close();
		}
	}
	
	public void delResumeById() throws Exception{
		HttpServletResponse response = ServletActionContext.getResponse();//取得reponse实例
		response.setCharacterEncoding("utf-8");//设置输出的编码
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();//得到printwriter实例
		HttpServletRequest request=ServletActionContext.getRequest();
		String userName = request.getParameter("userName");
		System.out.println("------------->+"+userName);
		System.out.println("THIS IS ResumeUser  1*********");
		StringBuilder sb = new StringBuilder();
		BufferedReader reader = request.getReader();
		char[]buff = new char[1024];
		int len;
		while((len = reader.read(buff)) != -1) {
			  sb.append(buff,0, len);
		}
		System.out.println(sb.toString());
		String mark = sb.toString();
		System.out.println(mark);
		System.out.println(mark.length());
		JSONObject dataJson = new JSONObject(mark);
		int resumeId = dataJson.getInt("resumeId");
		//String resumeId=dataJson.getString("state");
		try{
			System.out.println("THIS IS ResumeUser  1*********");
			out.println(this.resumelogic.delResumeById(resumeId));
			//out.println(this.resumelogic.getResumuByName(userName));
			//out.print(this.resumelogic.getAllResume(jobid));
			System.out.println("THIS IS ResumeUSer  2*********");
		}catch (Exception e) {
			e.printStackTrace();
		} finally {
			out.flush();
			out.close();
		}
	}
}



